package com.mk.Service.Impl;

import com.mk.Entity.Tag;
import com.mk.Entity.Type;
import com.mk.Entity.queryObject;
import com.mk.Mapper.TagMapper;
import com.mk.Service.ITagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class TagServiceImpl implements ITagService {

    @Autowired
    TagMapper tagMapper;

    @Override
    public List<Tag> queryAll() {
        List<Tag> tagList=tagMapper.selectAll();
        return tagList;
    }

    @Override
    public List<Tag> queryAllLimit(queryObject queryObject) {
        List<Tag> tagList = tagMapper.selectAllLimit(queryObject);
        return tagList;
    }

    @Override
    public List<Tag> queryBlogTag(Long bid) {
        List<Tag> tagList=tagMapper.selectBlogTag(bid);
        return tagList;
    }

    @Override
    public List<Tag> queryTagString(String ids) {
        List<Long> idList=convert(ids);
        if(idList!=null && idList.size()>0){
//            idList有数据再查询，否则报错
            List<Tag> tags=tagMapper.selectByList(idList);
            return tags;
        }
        return null;
    }

    @Override
    public void insertTag(Tag tag) {
        tagMapper.insert(tag);
    }

    @Override
    public void deleteTag(Long id) {
        tagMapper.delete(id);
    }

    @Override
    public void updateTag(Tag tag) {
        tagMapper.update(tag);
    }

    @Override
    public Integer queryCount() {
        Integer count=tagMapper.selectCount();
        return count;
    }


    private List<Long> convert(String ids){
        List<Long> list=new ArrayList<>();
        if(!"".equals(ids) && ids!=null){
            String[] strList=ids.split(",");
            for (int i = 0; i < strList.length; i++) {
                list.add(Long.valueOf(strList[i]));
            }
        }
        return list;
    }
}
